using System;
using System.Data;
using DotNetNuke.Framework.Providers;
using Microsoft.ApplicationBlocks.Data;

namespace SE.HTMLRepeater.Components
{
    public class SqlDataProvider : DataProvider
    {


        #region vars

        private const string providerType = "data";
        private const string moduleQualifier = "Softwaresedge_";

        private ProviderConfiguration providerConfiguration = ProviderConfiguration.GetProviderConfiguration(providerType);
        private string connectionString;
        private string providerPath;
        private string objectQualifier;
        private string databaseOwner;

        #endregion

        #region cstor

        /// <summary>
        /// cstor used to create the sqlProvider with required parameters from the configuration
        /// section of web.config file
        /// </summary>
        public SqlDataProvider()
        {
            Provider provider = (Provider)providerConfiguration.Providers[providerConfiguration.DefaultProvider];
            connectionString = DotNetNuke.Common.Utilities.Config.GetConnectionString();

            if (connectionString == string.Empty)
                connectionString = provider.Attributes["connectionString"];

            providerPath = provider.Attributes["providerPath"];

            objectQualifier = provider.Attributes["objectQualifier"];
            if (objectQualifier != string.Empty && !objectQualifier.EndsWith("_"))
                objectQualifier += "_";

            databaseOwner = provider.Attributes["databaseOwner"];
            if (databaseOwner != string.Empty && !databaseOwner.EndsWith("."))
                databaseOwner += ".";
        }

        #endregion

        #region properties

        public string ConnectionString
        {
            get { return connectionString; }
        }


        public string ProviderPath
        {
            get { return providerPath; }
        }

        public string ObjectQualifier
        {
            get { return objectQualifier; }
        }


        public string DatabaseOwner
        {
            get { return databaseOwner; }
        }

        #endregion

        #region private methods

        private string GetFullyQualifiedName(string name)
        {
            return DatabaseOwner + ObjectQualifier + moduleQualifier + name;
        }

        private object GetNull(object field)
        {
            return DotNetNuke.Common.Utilities.Null.GetNull(field, DBNull.Value);
        }

        #endregion

        #region override methods

        public override IDataReader GetHTMLRepeaters(int moduleId)
        {
            return (IDataReader)SqlHelper.ExecuteReader(connectionString, GetFullyQualifiedName("GetHTMLRepeaters"), moduleId);
        }

        public override IDataReader GetHTMLRepeater(int moduleId, int itemId)
        {
            return (IDataReader)SqlHelper.ExecuteReader(connectionString, GetFullyQualifiedName("GetHTMLRepeater"), moduleId, itemId);
        }

        public override void AddHTMLRepeater(int moduleId, string content, int DisplayOrder, int userId, string AnchorText)
        {
            SqlHelper.ExecuteNonQuery(connectionString, GetFullyQualifiedName("AddHTMLRepeater"), moduleId, content,DisplayOrder, userId,AnchorText);
        }

        public override void UpdateHTMLRepeater(int moduleId, int itemId, string content, int DisplayOrder, int userId, string AnchorText)
        {
            SqlHelper.ExecuteNonQuery(connectionString, GetFullyQualifiedName("UpdateHTMLRepeater"), moduleId, itemId, content, DisplayOrder, userId, AnchorText);
        }

        public override void DeleteHTMLRepeater(int moduleId, int itemId)
        {
            SqlHelper.ExecuteNonQuery(connectionString, GetFullyQualifiedName("DeleteHTMLRepeater"), moduleId, itemId);
        }

        public override int GetNextHTMLRepeaterOrder(int moduleId)
        {
            return Convert.ToInt16(SqlHelper.ExecuteScalar(connectionString, GetFullyQualifiedName("GetNextHTMLRepeaterOrder"), moduleId));
        }

        public override int GetHTMLRepeatersCount(int moduleId)
        {
			object Count = SqlHelper.ExecuteScalar(connectionString, GetFullyQualifiedName("GetNextHTMLRepeaterOrder"), moduleId);
			
			if (!Count.GetType().IsPrimitive)
				Count = 0;

			return Convert.ToInt16(Count);
        					
        }

        public override IDataReader GetHTMLRepeatersPaged(int moduleId, int pageNumber, int pageSize)
        {
            return (IDataReader)SqlHelper.ExecuteReader(connectionString, GetFullyQualifiedName("GetHTMLRepeatersPaged"), moduleId, pageNumber,pageSize);
        }

        #endregion
    }
}
